﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class BusInfoController : Controller
    {
        //
        // GET: /BusInfo/

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;

            var qry = GswTblBusInfo.GetBusInfos(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }

        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var busInfo = id == 0
                ? new GswTblBusInfo {StartDate = DateTime.Today, EndDate = DateTime.Today}
                : GswTblBusInfo.SingleOrDefault(id);
            busInfo.BaseInfos = GswTblBaseInfo.Fetch("where FunctionName='线路类别' and CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                     " order by FunctionName,TypeNo");
            return View(busInfo);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(int id ,FormCollection c, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var busInfo = id == 0
                ? new GswTblBusInfo()
                : GswTblBusInfo.SingleOrDefault(id);
            var baseInfos = GswTblBaseInfo.Fetch("where FunctionName='线路类别' and CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                   " order by FunctionName,TypeNo");
            busInfo.StartDate = DateTime.Parse(c["StartDate"]);
            busInfo.StartTime = c["StartTime"];
            busInfo.OriginPlace = c["OriginPlace"];
            busInfo.EndDate = DateTime.Parse(c["EndDate"]);
            busInfo.EndPlace = c["EndPlace"];
            busInfo.RouteLine = c["RouteLine"];
            busInfo.Seats = InputValid.StrToInt(c["Seats"], 0);
            busInfo.Remark = c["Remark"];
            busInfo.Price = InputValid.StrToDecimal(c["Price"], 0);
            busInfo.BusTypeId = InputValid.StrToInt(c["BusTypeId"], 0);
            busInfo.BusType = GswTblBaseInfo.GetMasterTypeName(baseInfos,
                InputValid.StrToInt(c["PayTypeId"], 0));
            busInfo.ParentId = GswTblUser.CurrentUser.CustomerId;
            try
            {
                if (id == 0)
                {
                    busInfo.Insert();
                    busInfo = new GswTblBusInfo {StartDate = DateTime.Today, EndDate = DateTime.Today};
                }
                else
                {

                    busInfo.Update();
                }
                ViewBag.scode = "1";
            }
            catch (Exception e)
            {

                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            busInfo.BaseInfos =baseInfos;
            return View(busInfo);
        }

        public ActionResult Delete(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblBusInfo.Delete(id);
            var qry = GswTblBusInfo.GetBusInfos(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId);
            return View("Index",qry);
        }


        public ActionResult Reserve(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;

            var qry = GswTblBusInfo.GetBusInfos(sdate, edate, keywords, GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult ReserveManager(int id = 0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var busInfo = GswTblBusInfo.SingleOrDefault(id);
            busInfo.BusBooks = GswViewBusBook.Fetch("where BusId=" + id + " order by id desc");
            return View(busInfo);
        }
        [HttpPost]
        public ActionResult ReserveManager(int id ,FormCollection c, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var busInfo = GswTblBusInfo.SingleOrDefault(id);
            var books = GswViewBusBook.Fetch("where BusId=" + id + " order by id desc");
            int num = 0;
            try
            {
                foreach (var item in books)
                {
                    if (string.IsNullOrEmpty(c["chk" + item.Id])) continue;
                    if (InputValid.StrToInt(c["chk" + item.Id], 0) > 0)
                    {
                        //num += item.BookNum;
                        num++;
                        GswTblBusBook.Update("set ConfirmDate='" + DateTime.Today.ToShortDateString() +
                                             "',ConfirmFlag=1 where id=" + item.Id);
                    }
                }
                GswTblBusInfo.Update("set ReserveNum=ReserveNum+" + num + " where id=" + id);
            }
            catch (Exception e)
            {

                ModelState.AddModelError("", e.Message);
            }
          
            busInfo.BusBooks = GswViewBusBook.Fetch("where BusId=" + id + " order by id desc");
            return View(busInfo);
        }
        public ActionResult ReserveDelete(int id = 0,int rid=0, string m = "", string f = "", string p = "", string fid = "", string r = "", string sdate = "", string edate = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.r = r;
            ViewBag.sdate = sdate;
            ViewBag.edate = edate;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var books = GswTblBusBook.SingleOrDefault(rid);
            books.ConfirmDate = "";
            books.ConfirmFlag = 0;
            books.Update();
            GswTblBusInfo.Update("set ReserveNum=ReserveNum-" + books.BookNum + " where id=" + id);
            var busInfo = GswTblBusInfo.SingleOrDefault(id);
            busInfo.BusBooks = GswViewBusBook.Fetch("where BusId=" + id + " order by id desc");
            return View("ReserveManager",busInfo);
        }
    }
}
